<?php

require_once './core/p_pre.php';
process($db, $userId, $deviceId);
require_once './core/p_post.php';

function process(&$db, $userId, $deviceId) {
	require_once './core/dataset.php';
	$keyPage;
	if ( param('v') == 'm' ) {
		$keyPage = 'key_m.php';
	} else {
		$keyPage = 'key_d.php';
	}
	$kingdom = param('kingdom');
	$continent = param('continent');
	
	$sqlCnt;
	$sql;
	$where = "";
	
	$searchTerm = param('searchTerm');
	if ( strlen($searchTerm) > 0 ) {
		$searchTerm = sqlEscape($searchTerm);
	}
	$sqlCnt;
	$sql;
	$isLanguage = false;
	$lang = param('lang');
	if ( strlen($lang) > 0 ) {
		$isLanguage = true;
		$sqlCnt = "SELECT count(*) cnt FROM key_data WHERE 
				id in (SELECT datasetId from key_message where language = '$lang')";
		$sql = "SELECT d.id, d.inkUrl, m.title label, CONCAT('translated by ', m.author) title  
				FROM key_data d, key_message m
				WHERE m.language = '$lang' AND m.datasetId = d.id";
	} else {
		$sqlCnt = "SELECT count(*) cnt FROM key_data";
		$sql = "SELECT * FROM key_data ";		
		$where = CROUtil::join($where, "Length (dataDir) > 0", 'AND');
		if ( strlen($kingdom) > 0 ) {
			$where = CROUtil::join($where, "kingdom = '$kingdom'", 'AND');
		}
		if ( strlen($continent) > 0 ) {
			$where = CROUtil::join($where, "continent = '$continent'", 'AND');
		}
		
		if (strlen($searchTerm) > 0) {
			$where = CROUtil::join($where, "label like '$searchTerm%'", 'AND');
		}
		
		if ( $where ) {
			$sql .= " WHERE $where";
			$sqlCnt .= " WHERE $where";
		}
	}

	
//echo $sqlCnt;	
		
	$totalCnt = $db->selectCnt($sqlCnt);
	if ( $totalCnt < 1 ) {
		return;
	}
	
	$orderBy = param('orderBy');
	if ( strlen($orderBy) < 1 ) {
		$orderBy = "label";
	}
	$sql .= " ORDER BY $orderBy";

	$limitCount = param('limitCount');
	if ( !$limitCount ) {
		$limitCount = 10000;
	}
	if ( $limitCount ) {
		$limitStart = param('limitStart');
		if ( !$limitStart ) {
			$limitStart = '0';
		}
		$limit = " LIMIT $limitStart, $limitCount";
	}
	$sql .= " $limit";
	

	
	$result =& $db->doQuery($sql);
	$list = array();
	$cnt = 0;
	if ( $result ) {		
		while($row = mysql_fetch_array($result)){
			$r = new DataSet();
			$r->id = $row["id"];
			$r->inkURL = checkGet($row, 'inkUrl');
			$r->label = checkGet($row, 'label');
			if ( strlen($r->label) < 1 ) {
				$r->label = ' ';
			}
			$r->title = checkGet($row, 'title');			
			$r->subTitle = checkGet($row, 'subTitle');			
			$r->description = checkGet($row, 'description');
			$r->imagePath = checkGet($row, 'imagePath');
			$r->dataDir = checkGet($row, 'dataDir');
			$list[$cnt] = $r;
			
			$cnt++;
		}
	}
	
	$len = count($list);
	if ( param('format') == 'json' ) {
		echo '{"total":'.$totalCnt.',"dataList":'.json_encode($list).'}';
		return;
	}
	
	$title;
	if ( strlen($kingdom) > 0 ) {
		$title = $kingdom;
	} else if ( strlen($continent) > 0 ) {
		$title = $continent;
	} else {
		$title = 'Data Sets';
	}
	

?>

<div class="contentTitle"><?php echo $title; ?></div><br/>
<ul data-role="listview" data-filter="true">
<?php 
$prevFirstLetter = '';
$langURL = '';
if ( $isLanguage > 0 ) {
	$langURL = '&lang='.$lang;
}
for ($i=0; $i<$len; $i++) {
	$d = $list[$i];
	$firstLetter = strtoupper($d->label[0]);
	$url = $keyPage.'?datasetId='.$d->id.$langURL;
?>

<?php if ( !$isLanguage && $prevFirstLetter != $firstLetter )  {
	$prevFirstLetter = $firstLetter;
	?>
	<li data-role="list-divider"><?php echo $prevFirstLetter; ?></li>
<?php } ?>

<li>
<a data-ajax="false" href="<?php echo $url; ?>">
<h3 class="uDataSetTitle"><?php echo $d->label; ?></h3>
<p>
<?php echo $d->inkURL; ?>
<br/>
<?php if ( strlen($d->title) ) {?>
<?php echo htmlspecialchars($d->title); ?>
<?php } ?>
</p>
</a>

<br/>
<a href="javascript:ULoader.loadPage('graph.php?datasetId=<?php echo $d->id;?>')"
>Graph</a>

</li>

	
<?php }	?>

</ul>


<?php } ?>
